//
//  RefreshBaseTableViewController.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/25.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "RefreshBaseTableViewController.h"


@interface RefreshBaseTableViewController ()<UIScrollViewDelegate>


@end

@implementation RefreshBaseTableViewController


- (void) setCanLoadMore:(BOOL)canLoadMore
{
    
    
    
    if (_canLoadMore == NO) {
        
        
        
        //[self performSelector:@selector(test) withObject:nil afterDelay:3];
        

    }
    _canLoadMore = canLoadMore;
}


- (void) setServerURL:(NSString *)serverURL WithCacheURL:(NSString *)cacheURL
{
    _serverURL = serverURL;
    _cacheURL = cacheURL;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.initialized = 0;
    self.canLoadMore = NO;
    
    [self loadDataInServer];
    
    //下拉刷新
    [self creatRefreshing];
    //上拉加载
    [self createAddData];
    
}


- (void)createNavigationWithChannel
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 60, 40);
    [leftButton setTitleColor:[UIColor colorWithRed:29/255. green:103/255. blue:255/255. alpha:1] forState:UIControlStateNormal];
    [leftButton setTitle:@"  返回" forState:UIControlStateNormal];
    [leftButton setImage:[[UIImage imageNamed:@"back@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    view.backgroundColor = [UIColor blueColor];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [leftButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    AROnlineCountView *rightView = [[AROnlineCountView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    self.rightView = rightView;
    
    self.rightView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onRightButtonClick)];
    [self.rightView addGestureRecognizer:tap];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}

- (void)onRightButtonClick
{
    //NSLog(@"a");
    
}

- (void) backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void) createAddData
{
    UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    self.tableView.tableFooterView = ac;
    self.ac = ac;
    ac.color = [UIColor darkGrayColor];
    [ac stopAnimating];
    ac.hidden = YES;
}

- (void) loadDataInCache
{
    //清空数据
    self.dataArray = nil;
    self.canLoadMore = YES;

}

- (void) loadDataInServer
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void) loadDataMore
{
    self.ac.hidden = NO;
    [self.ac startAnimating];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}


- (void)creatRefreshing {
    self.refreshControl = [[UIRefreshControl alloc] init];
    //self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"努力加载中……"];
    self.refreshControl.tintColor = [UIColor grayColor];
    [self.refreshControl addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
}

- (void) refreshAction
{
    [self loadDataInServer];
}

- (void) loadDataInServerComplete
{
    self.initialized = 0;
    self.canLoadMore = YES;
    self.ac.hidden = YES;
    [self.ac stopAnimating];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    
    [self.refreshControl endRefreshing];
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

    float reload_distance = 5;
    if(y > h + reload_distance) {
        
        
           
            
            if (self.canLoadMore == YES) {
                self.canLoadMore = NO;
                NSLog(@"load more rows");
                [self loadDataMore];

            }
  
        
    }
}





#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
